/***************************************************************************
    copyright            : Kashyap R Puranik
    email                : kashthealien@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "nuclearCalculator.h"
#include <math.h>

nuclearCalculator::nuclearCalculator()
{	
	m_halfLife = 0.0;
	m_initAmount  = 0.0;
	m_finalAmount = 0.0;
	m_time = 0.0;
}

nuclearCalculator:: ~nuclearCalculator()
{

}

double nuclearCalculator::calculateInitAmount ( void )
{
	double amount = m_finalAmount;
	double time = m_time;
	while ( time > m_halfLife )
	{
		time -= m_halfLife;
		amount *= 2.0;
	}
	
	amount *= pow ( 2.0 , time / m_halfLife );

	return amount;
}

double nuclearCalculator::calculateFinalAmount ( void )
{
	double amount = m_initAmount;
	double time = m_time;
	while ( time > m_halfLife )
	{
		time -= m_halfLife;
		amount /= 2.0;
	}
	
	amount /= pow ( 2.0, time / m_halfLife );
	
	return amount;
}

double nuclearCalculator::calculateTime ( void )
{
	double ratio = m_initAmount / m_finalAmount;		// calculate the ratio of final to initial
	double numHalfLives = log ( ratio )/ log ( 2.0 );	// The number of halfLives ( log 2 ( x ) = log x / log 2 )
	double time = numHalfLives * m_halfLife;			// The total time taken
	
	return time;
}
